<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace addons\config\user\controller;

/**
 * Description of News
 * 签到设置
 * @author shilinqing
 */
class SignConf extends \web\user\controller\AddonUserBase{

    public function index(){
        return $this->fetch();
    }

    /**
     * 签到列表
     * @return array
     */
    public function loadList(){
        $m = new \addons\config\model\SignConf();
        $total = $m->getTotal();
        $rows = $m->getDataList($this->getPageIndex(),$this->getPageSize(), '');
        return $this->toDataGrid($total, $rows);
    }

    /**
     * 添加签到规则
     */
    public function news_post() {
        $m = new \addons\config\model\SignConf();
//        $max_data = $m->field('days,amount')->order('days desc')->find();
        if (IS_POST) {
            $data['amount'] = $this->_post('amount');
            $data['days'] = $this->_post('days');
            $data['update_time'] = NOW_DATETIME;
            try {
                $data['id'] = $this->_post('id');
                if ($data['id']){
                    $ret = $m->save($data);
                }else{
                    $ret = $m->add($data);
                }
//                if($data['amount'] < $max_data['amount']) return $this->failData('添加的金额少于当前最高金额');
                if(!$ret){
                    return $this->failData('添加失败');
                }
                return $this->successData();
            } catch (\Exception $e) {
                return $this->failData($e->getMessage());
            }
        } else {
            $id = $this->_post('id');
            $max_data = $m->field('days,amount')->order('days desc')->find();
            $this->assign('days',$max_data['days']);
            $this->assign('id',$id);
            $this->setLoadDataAction('loadData');
            return $this->fetch();
        }
    }
    /*
     * 获取数据
     */
    public function loadData()
    {
        $id = $this->_get('id');
        $m = new \addons\config\model\SignConf();
        $data = $m->getDetail($id);
        return $data;
    }

    /**
     * 删除签到规则
     */
    public function del() {
        $id = intval($this->_get('id'));
        if (!empty($id)) {
            $m = new \addons\config\model\SignConf();
            $max_data = $m->field('days,amount')->order('days desc')->find();
            $max_days = $m->where(['id' => $id])->value('days');
            if($max_days < $max_data['days']){
                return $this->failData('请从最大的那天开始删除');
            }else{
                try {
                    $res = $m->deleteData($id);
                    if ($res > 0) {
                        return $this->successData();
                    } else {
                        return $this->failData('删除失败');
                    }
                } catch (\Exception $e) {
                    return $this->failData($e->getMessage());
                }
            }
        } else {
            return $this->failData('删除失败，参数有误');
        }
    }


}
